from ttkbootstrap.dialogs.dialogs import Messagebox

from openpyxl import Workbook


def center_window(window):
    """ 窗口居中 """
    # 更新窗口以获取其实际尺寸
    window.update_idletasks()

    # 获取屏幕的宽度和高度
    screen_width = window.winfo_screenwidth()
    screen_height = window.winfo_screenheight()

    # 获取窗口的宽度和高度
    window_width = window.winfo_width()
    window_height = window.winfo_height()

    # 计算窗口左上角的坐标
    x = (screen_width - window_width) // 2
    y = (screen_height - window_height) // 2

    # 设置窗口的位置
    window.geometry(f"{window_width}x{window_height}+{x}+{y}")

def export_excel(headers: list, data: list, file_path: str):
    """ 导出Excel，参数说明：
            headers (list)：Excel表头
            data (list)：导出数据
            file_path (str)：Excel文件路径
    """
    # 创建一个工作簿对象
    workbook = Workbook()
    # 获取活动工作表
    sheet = workbook.active
    # 定义表头
    sheet.append(headers)

    # 将数据逐行添加到工作表中
    for row in data:
        sheet.append(row)

    # 保存工作簿
    try:
        workbook.save(file_path)
        Messagebox.ok("数据导出完成", title="消息")
    except Exception as e:
        Messagebox.show_error("数据导出出错，请稍后重试", title="提示")